package com.bgds.sys.module.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import com.bgds.sys.api.bean.mars.dto.DataTimeSerise;
import com.bgds.sys.api.service.DeTongService;
import com.bgds.sys.influx.service.InfluxService;
import com.bgds.sys.module.entity.BaseDataTimeSerise;
import com.bgds.sys.module.entity.Device;
import com.bgds.sys.module.entity.DeviceDynamicAttr;
import com.bgds.sys.module.entity.vo.PumpSocketVo;
import com.bgds.sys.module.mapper.DeviceMapper;
import com.bgds.sys.module.service.IDeviceDynamicAttrService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author chenchao
 * @date 2021/10/28
 */
@RestController
public class BaseBaoxing {

    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    @Autowired
    private IDeviceDynamicAttrService deviceDynamicAttrService;
    @Autowired
    private InfluxService influxService;

    @Autowired
    private DeTongService deTongService;
    @Resource
    private DeviceMapper deviceMapper;

    @RequestMapping("/test/{code}")
    public Map all(@PathVariable  String code) {
        String deviceCode = code;
        List<DeviceDynamicAttr> dynamicAttrs = deviceDynamicAttrService.listByDeviceCode(deviceCode, null);
        List<DeviceDynamicAttr> filterAttrs = dynamicAttrs.stream().filter(x -> StringUtils.isNotBlank(x.getGroupCode())).collect(Collectors.toList());
        //获取除磷泵属性分组
        Map<String, List<DeviceDynamicAttr>> pumpAttrs = filterAttrs.stream().collect(Collectors.groupingBy(DeviceDynamicAttr::getGroupCode, LinkedHashMap::new, Collectors.toList()));
        //返回map
        Map<String, List<PumpSocketVo>> mapVo = new HashMap<>(10);
        DecimalFormat df = new DecimalFormat("#.00");
        for (Map.Entry<String, List<DeviceDynamicAttr>> entry : pumpAttrs.entrySet()) {
            List<Map<String, Object>> maps = influxService.queryByForMap(deviceCode, entry.getValue());
            Map<String, Object> all = new HashMap<>(5);
            for (Map<String, Object> map : maps) {
                all.putAll(map);
            }
            if (CollectionUtil.isNotEmpty(maps)) {
                List<PumpSocketVo> pumpSocketVos = new ArrayList<>();
                for (DeviceDynamicAttr attr : entry.getValue()) {
                    PumpSocketVo pumpSocketVo = new PumpSocketVo();
                    pumpSocketVo.setName(attr.getName());
                    Object o = all.get(attr.getEngName());
                    if (o != null) {
                        pumpSocketVo.setValue(Double.valueOf(df.format(Double.valueOf(o.toString()))));
                    }
                    pumpSocketVo.setState(0);
                    pumpSocketVo.setUnit(attr.getUnit());
                    pumpSocketVos.add(pumpSocketVo);
                }
                mapVo.put(entry.getKey(), pumpSocketVos);
            }
        }
        return mapVo;
    }

    @RequestMapping("/getObject")
    public List<BaseDataTimeSerise>  getObject(String relationId, Integer code) {
        List<DataTimeSerise> object = deTongService.getObject(relationId, code);
        List<BaseDataTimeSerise> base = Convert.convert(new TypeReference<List<BaseDataTimeSerise>>(){
        },object);
        Map<String, String> deviceList = deviceMapper.selectList(null).stream().collect(
                Collectors.toMap(Device::getRelationId, Device::getName,(v1,v2) -> v1));
        base.forEach(item -> {
            if (StrUtil.isNotBlank(item.getId())) {
                item.setName(deviceList.get(item.getId()));
            }
        });
        return base;
    }




}
